import { Prefab, find, instantiate, resources , Node } from "cc";
import { LoadMg } from "./LoadMg";
import { TipBox } from "./TipBox";
import { TipMg } from './TipMg';
import UtilTool from "./UtilTool";
import { SelectListPanel } from "../hall/SelectListPanel";
import { ErrorCode } from "../net/ErrorCode";
import { App } from "../App";
import { InvitePanel } from "../hall/InvitePanel";
import { NoticePanel } from "../hall/NoticePanel";
import { ChangeScore } from "../game/billiards/ChangeScore";
import { ScoreParticle } from "../game/billiards/ScoreParticle";
import { RewardBox } from "../hall/RewardBox";

type node = Node
const loadPreFab = ():Promise<node> => {
    return new Promise((resolove,reject) =>{
        resources.load('prefabs/publicBox',Prefab,(err,prefabs) =>{
            let box = instantiate(prefabs);
            find('Canvas').addChild(box);
            resolove(box)
        })
    })
}

export const loadCommonPreFab = (path,parentNode = null, isShowTip=true):Promise<node> => {
    if(isShowTip) {
        LoadMg.showLoad({type:1})
    }
    return new Promise((resolove,reject) =>{
        resources.load(path,Prefab,(err,prefabs) =>{
            let box = instantiate(prefabs);
            if (parentNode) {
                parentNode.addChild(box);
            } else {
                find('Canvas').addChild(box);
            }
            if(isShowTip) {
                LoadMg.removeLoad()
            }
            
            resolove(box)
        })
    })
}


//签到弹框
// export const signInBoxPopuUp  =  async () =>{
//     let node = await loadPreFab()
//     node.addComponent(signInBox)
// }


//文本提示
export const showTip = async(mes) =>{
    let node = await loadCommonPreFab('prefabs/common/tipMg', null, false)
    node.getComponent(TipMg).setDesData(mes)
}

// 显示错误提示
export const showErrorTip = (errorCode : number)=>{
    let errorInfo = ErrorCode[errorCode]
    if(errorInfo) {
        let textId = errorCode + 10000 // 错误码文本id为错误码+10000
        let textTip = App.language.getLanguageLab(textId)
        if(textTip == "") {
            textTip = App.language.getLanguageLab(9998) + errorCode // 错误码:xxx
        }
        showTip(textTip)
    } else {
        // 未知错误
        let textTip = App.language.getLanguageLab(9999) + ":" + errorCode
        showTip(textTip) // 未知错误:xxx
        console.error("未找到错误码", errorCode)
    }
}

//文本提示框
export const showTipBox = async(data) =>{
    let content = data.content || ""
    let btnType = data.btnType || 1
    let btnTexts = [App.language.getLanguageLab(38),App.language.getLanguageLab(2027)]
    btnTexts[0] = data.btnTexts1 || btnTexts[0]
    btnTexts[1] = data.btnTexts2 || btnTexts[1]
    

    let node = await loadCommonPreFab('prefabs/common/tipBox')
    node.getComponent(TipBox).setDesData({content:content, btnType:btnType, func:data.func, btnTexts:btnTexts})
    return node
}


//引导弹窗
export const setGuidePopuUp = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/guide',parent)
    return node
}

//设置语言弹窗
export const setLanguagePopuUp = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/setLanguage',parent)
    return node
}

// 显示我的界面
export const showMinePanel = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/minePanel/minepanel',parent)
    return node
}

// 显示邀请界面
export const showInvitePanel = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/invite')
    return node
}

//设置弹窗
export const setSettingPopuUp = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/setting',parent)
    return node
} 

//幸运台球二级界面弹窗
export const showLuckyBall = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/luckyBall/lb_Main' ,parent)
    return node
}

//幸运台球规则界面弹窗
export const showLuckyBallRule = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/luckyBall/lb_rule' ,parent)
    return node
}

//幸运台球记录界面弹窗
export const showLuckyBallRecord = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/luckyBall/lb_record' ,parent)
    return node
}

//幸运台球下注界面弹窗
export const showLuckyBallBet = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/luckyBall/lb_bet' ,parent)
    return node
}

//幸运台球开奖界面弹窗
export const showLuckyBallLottery = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/luckyBall/lb_lottery' ,parent)
    return node
}

//幸运台球走势界面弹窗
export const showLuckyBallTrend = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/luckyBall/lb_trend' ,parent)
    return node
}

// 弹出选项列表
export const showSelectList = async(parent, textList, cb : Function) =>{
    let node = await loadCommonPreFab('prefabs/common/selectListNode' ,parent)
    node.getComponent(SelectListPanel).initData(textList, cb)
    return node
}

//大厅记录界面弹窗
export const showHallRecord = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/record' ,parent)
    return node
} 

//大厅钱包界面弹窗
export const showHallWallet = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/wallet' ,parent)
    return node
} 

//系统公告界面弹窗
export const showSystemNotice = async(parent,obj) =>{
    let node = await loadCommonPreFab('prefabs/hall/notice' ,parent)
    node.getComponent(NoticePanel).initData(obj)
    return node
}

//大厅任务界面弹窗
export const showHallTask = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/task/task' ,parent)
    return node
} 

//大厅商城界面弹窗
export const showHallShop = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/hall/shop/shop' ,parent)
    return node
} 



//多人场玩家详情界面弹窗
export const showGameMulitUserDetail = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/game/layer3/UserDetails' ,parent)
    return node
} 

//多人场玩家认输界面弹窗
export const showGameMulitExit = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/game/layer3/GameMulitExit' ,parent)
    return node
} 

//游戏调整击打作用点弹窗
export const showGameActionPoint = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/game/actionPoint' ,parent)
    return node
}

//四球开伦猜拳弹窗
export const showGameFourMora = async(parent) =>{
    let node = await loadCommonPreFab('prefabs/game/layer4/GameMora' ,parent)
    return node
}

/**
 * 得分飘窗 
 * @param data.score 得分
 * @param data.pos 位置
 * @param data.tip 提示语
 * @param data.func 回调
 */
export const showChangeScore = async(data, parent= null) =>{
    let node = await loadCommonPreFab('prefabs/game/changeScore', parent, false)
    node.getComponent(ChangeScore).init(data.score,data.pos,data.tip,data.func)
    return node
}

/**
 * 得分粒子特效
 * @param startPos 开始位置
 * @param endPos 结束位置
 * @param func 回调
 */
export const showScoreParticle = async(data, parent= null) =>{
    let node = await loadCommonPreFab('prefabs/game/scoreParticle', parent, false)
    node.getComponent(ScoreParticle).init(data.startPos,data.endPos,data.type,data.func)
    return node
}

//通用恭喜获得奖励界面弹窗
export const showRewardBox = async(type, count, parent = null) =>{
    let node = await loadCommonPreFab('prefabs/hall/rewardBox' ,parent)
    node.getComponent(RewardBox).initData(type, count)
    return node
}